package org.redis.cluster.transport.netty.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import org.apache.commons.lang3.ArrayUtils;
import org.redis.cluster.transport.protocol.ProtocolEncoder;

public class NettyEncoder extends MessageToByteEncoder<Object> {
	
	private ProtocolEncoder encoder;
	
	public NettyEncoder(ProtocolEncoder encoder) {
		this.encoder=encoder;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
		byte content[]=encoder.encode(msg);
		if(ArrayUtils.isEmpty(content)) {
			return;
		}
		
		out.writeBytes(content);
	}

}